<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%! int no_of_page,row_per_page=5,pages,start_row_count,end_row_count;String page_name="dividePage.jsp"; Connection con=null; %>
<%
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select count(*) from khachhang");
int total=0;
if(rs.next())
total=rs.getInt(1);

if(total%5==0)
no_of_page=total/5;
else
no_of_page=total/5+1;
 
%>
<table width="658" >
<tr>
<th>
<h2>
<%
for(int i=1;i<=no_of_page;i++)
{
out.println("<a href='"+page_name+"?pages="+i+"'>"+i+"</a>");
}
%>
</h2></th>
</tr>
</table>

<%
String pages1=request.getParameter("pages");
if(pages1!=null)
{
pages=Integer.parseInt(pages1);
}
if(pages==0) pages=1;
if(pages==1)
{
start_row_count=1;
}
else
{
start_row_count=(pages-1)*5;
}

end_row_count=pages*5;

String query="Select (Select count(*) from khachhang counter where counter.id_person<=khachhang.id_person) as id_person,first_name,last_name,gioi_tinh,ngay_sinh,dia_chi,dien_thoai from khachhang where (Select count(*) from khachhang counter where counter.id_person<=khachhang.id_person) between "+start_row_count+" and "+end_row_count +" order by id_person";
ResultSet rs1=stmt.executeQuery(query);
%>